package com.manage.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.xmlbeans.impl.xb.xsdschema.RestrictionDocument.Restriction;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.manage.dao.IBaseDao;
import com.manage.po.Borrow;
import com.manage.service.IBorrowService;
import com.manage.util.PagerModel;



@Service("BorrowService")
@Transactional(propagation = Propagation.REQUIRED)
public class BorrowService implements IBorrowService{

	@Autowired
	private IBaseDao baseDao;
		
	@Override
	public Borrow getBorrowById(Integer borrowid) {
		return baseDao.get(Borrow.class, borrowid);
	}

	@Override
	public void merge(Borrow borrow) {
		baseDao.merge(borrow);
	}

	@Override
	public PagerModel getBorrowById(Integer borrowid, int pageno, int pagesize) {
		// TODO Auto-generated method stub
		//String sql = "select * from device where deviceid = " + "'" + deviceid + "'";
		String sql = "select * from borrow";
		System.out.println("********" + sql);
		return baseDao.findPageBySql(Borrow.class, sql, null, pageno, pagesize);
	}

	@Override
	public void save(Borrow borrow) {
		// TODO Auto-generated method stub
		baseDao.save(borrow);
	}

	@Override
	public void update(Borrow borrow) {
		// TODO Auto-generated method stub
		baseDao.update(borrow);
	}

	@Override
	public void delete(Integer borrowid) {
		// TODO Auto-generated method stub
		Borrow borrow = getBorrowById(borrowid);
		baseDao.delete(borrow);
	}

	@Override	public void batchDelete(Integer [] ids) {
		// TODO Auto-generated method stub
		for(int i=0;i<ids.length;i++)
			delete(ids[i]);	
	}
	
	@Override
	public PagerModel search(String borrowid,String deviceid,String borunittype, String returned ,String searchContact ,int pageno,int pagesize) {
		List<Criterion> cronlist = new ArrayList<Criterion>();
		if(borrowid!=null && !borrowid.equals(""))
			cronlist.add(Restrictions.eq("borrowid", Integer.parseInt(borrowid)));
		if(deviceid!=null && !deviceid.equals(""))
			cronlist.add(Restrictions.like("deviceid",deviceid,MatchMode.ANYWHERE));
		if(borunittype!=null && !borunittype.equals(""))
			cronlist.add(Restrictions.like("borunittype",borunittype,MatchMode.ANYWHERE));
		if(returned!=null && !"".equals(returned.trim())){
			if("是".equals(returned)){				
				cronlist.add(Restrictions.eqProperty("borcount", "retcount"));
			}
			else if("否".equals(returned)){
				cronlist.add(Restrictions.or(Restrictions.isNull("retcount"),Restrictions.gtProperty("borcount", "retcount")));
			}
		}
		if(searchContact!=null && !"".equals(searchContact)){
			cronlist.add(Restrictions.like("contact", searchContact ,MatchMode.ANYWHERE));
		}
		
		return baseDao.findPageByCriterion(Borrow.class, pageno, pagesize, null, cronlist);
	}

	@Override
	public Borrow TestHql(String borrowid) {
		// TODO Auto-generated method stub
		String hql="from Borrow where borrowid=?";//HQL语句中表名应该是ORM映射的类名,查询結果是实体类，而不是sql的结果集
		List<Borrow> list=baseDao.findByHql(hql, borrowid);
		return list.get(0);
	}

	@Override
	public Borrow TestHql(String... param) {
		String hql="from Borrow where borrowid=?";
		List<Borrow> list=baseDao.findByHql(hql, param);
		return list.get(0);
	}

}
